<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/4/4
 * Time: 10:45
 */

namespace app\admin\controller;
use app\admin\logic\RoleLogic;
use think\facade\Request;
use app\admin\logic\FleetLogic;
class Fleet extends Base{

    private $fleetLogic;
    private $roleLogic;

    public function __construct()
    {
        parent::__construct();
        $this->fleetLogic = new FleetLogic();
        $this->roleLogic = new RoleLogic();
    }
    public function read(){
        return $this->fetch();
    }
    /**
     * 管理员首页
     * @return mixed
     */
    public function index(){
        $role = session('admin_info');
        $this->assign('role',$role);
        return $this->fetch();
    }

    public function shop(){
        $role = session('admin_info');
        $this->assign('role',$role);
        $a = $this->getcode($role['id']);
        return $this->fetch();
    }

    public function info(){
        $id = Request::param('id');
        $act = 'add';
        $info = [];
        if ($id){
            $act = 'edit';
            $this->getcode($id);
            $info = $this->fleetLogic->info($id);
        }
        $roleList = $this->roleLogic->getAllRole();
        $this->assign('act', $act);
        $this->assign('info',$info);
        $this->assign('role_list', $roleList['data']);

        $role = session('admin_info');
        $this->assign('role',$role);
        return $this->fetch();
    }

    /**
     * 管理员异步列表
     * @return \think\response\Json
     */
    public function ajaxIndex(){
        $tempWhere = Request::param();
        $res = $this->fleetLogic->ajaxIndex($tempWhere, self::$nowPage);
        return json($res);
    }

    public function ajaxShop(){
        $tempWhere = Request::param();
        $res = $this->fleetLogic->ajaxShop($tempWhere, self::$nowPage);
        return json($res);
    }

    /**
     * 添加/编辑管理员
     * @return mixed
     */
    public function add(){
        $id = Request::param('id');
        $act = 'add';
        $info = [];
        if ($id){
            $act = 'edit';
            $info = $this->fleetLogic->info($id);
        }
        $roleList = $this->roleLogic->getAllRole();
        $this->assign('act', $act);
        $this->assign('info',$info);
        $this->assign('role_list', $roleList['data']);
        return $this->fetch();
    }

    /**
     * 处理管理员逻辑
     * @return \think\response\Json
     */
    public function handle(){
        $data = Request::param();
        $res = $this->fleetLogic->handle($data);
        return json($res);
    }

    public function getcode($shop_id){

        $APPID='wx299675cff7939ef8';
        $AppSecret='ac6d0583a3d27e9c8f506867e3cee21c';
        $qrpath='user'; //路径在public文件夹里面
        if(!is_dir('.'.$qrpath)){                              //  /public/qrcode/
            mkdir(iconv("GBK","UTF-8",'.'.$qrpath),0777,true);
        }
        $file = $qrpath.'/'.$shop_id;
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $fileUrl = $protocol.$_SERVER['HTTP_HOST'].$file;       //   http://XXXX/
        $savePath = '.'.$file;

        //获取access_token
        $access = json_decode($this->get_access_token($APPID,$AppSecret),true);
        $access_token= $access['access_token'];

        $url="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token;

        //传递参数，用$user_id区分每个用户生成的二维码
        $listdata=[
            'path'=>'pages/index/index?shop_id='.$shop_id
        ];
        $listdata=json_encode($listdata);
        $result=$this->api_notice_increment($url,$listdata);
        if (!$result) {
            return false;
        }

        //查询是否存在二维码
        $list='';
        if(empty($list['qrcode'])){
            $res = file_put_contents("upload/user/".$shop_id.".jpg", $result);
            //  将获取到的二维码图片流保存成图片文件
            if($res===false) return json_encode(array('status'=>0,'info'=>'生成二维码失败','res'=>$res));
            $filename="upload/user/".$shop_id.".jpg";//用id编号当二维码名称
            $datafile=['qrcode'=>$filename];
            db('admin')->where('id','eq',$shop_id)->update(['code'=>'/'.$filename]);
            return $filename;
        }else{
            return '';
        }
    }
    public function api_notice_increment($url, $data){
        $curl = curl_init();
        $ch = curl_init();
        $header = "Accept-Charset: utf-8";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        //curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            return false;
        }else{
            return $tmpInfo;
        }
    }
    public function get_access_token($APPID,$AppSecret){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$AppSecret;
        return $data = $this->curl_get($url);
    }
    public function curl_get($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        return $data;
    }

}